<?php
class Common_model extends CI_Model{
    function  __construct(){
        parent::__construct();
    }
    function getdata($table,$where='',$select='',$order='',$limit=''){
        if(!empty($where)){
            $this->db->where($where);
        }
        if(!empty($order)){
            $this->db->order_by($order[0],$order[1]);
        }
        if(!empty($limit)){
            if(is_array($limit)){
                $this->db->limit($limit[0],$limit[1]);
            }else $this->db->limit($limit);
        }
        if(!empty($select)){
            $this->db->select($select);
        }
        $query = $this->db->get($table);
        if($query->num_rows() > 0){
            $data = $query->result();
            $query->free_result();
            return $data; 
        }else return false;
    }
    function getdatain($table,$where=array(),$order='',$limit=''){
        if(!empty($where)){
            $this->db->where_in($where[0],$where[1]);
        }
        if(!empty($order)){
            $this->db->order_by($order[0],$order[1]);
        }
        if(!empty($limit)){
            if(is_array($limit)){
                $this->db->limit($limit[0],$limit[1]);
            }else $this->db->limit($limit);
        }
        if(!empty($select)){
            $this->db->select($select);
        }
        $query = $this->db->get($table);
        if($query->num_rows() > 0){
            $data = $query->result();
            $query->free_result();
            return $data; 
        }else return false;
    }
    function getone($table,$where='',$select=''){
        if(!empty($where)){
            $this->db->where($where);
        }
        if(!empty($select)){
            $this->db->select($select);
        }
        $query = $this->db->get($table);
        if($query->num_rows() > 0){
            $data = $query->row();
            $query->free_result();
            return $data;
        }else return false;
    }
    function insertdata($table,$data=array()){
        $this->db->insert($table, $data); 
    }
    function updatedata($table,$where ='',$data=array()){
        $this->db->where($where);
        $this->db->update($table, $data);
    }
    function deletedata($table,$where){
        $this->db->where($where);
        $this->db->delete($table);
    }
    function getnumber($table,$where = ''){
        if(!empty($where)){
            $this->db->where_in($where[0],$where[1]);
        }
        $query = $this->db->get($table);
        return $query->num_rows();
    }
    function getnumber2($table,$where=''){
        if(!empty($where)){
            $this->db->where($where);
        }
        return $this->db->count_all_results($table);
    }
    function getsum($fiel,$where,$table){
        $this->db->select_sum($fiel);
        $this->db->where($where);
        $query = $this->db->get($table);
        return $query->row()->$fiel;
    }
    function check_trung($nd,$table){
        $this->db->where($nd);
        $query = $this->db->get($table);
        if($query->num_rows()>0){
            $query->free_result();
            return true;
        }return false;
    }
}